package com.whfc.hdc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Preconditions;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.PageData;
import com.whfc.common.util.CollectionUtil;
import com.whfc.common.util.PageUtil;
import com.whfc.hdc.dao.AppDevicePlatformMapper;
import com.whfc.hdc.dto.AppDevicePlatformDTO;
import com.whfc.hdc.entity.AppDevicePlatform;
import com.whfc.hdc.service.AppDevicePlatformService;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @author: xugcheng
 * @version: 1.0
 * @date: 2021-11-30 15:05
 */
@DubboService(interfaceClass = AppDevicePlatformService.class, version = "1.0.0")
public class AppDevicePlatformServiceImpl implements AppDevicePlatformService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AppDevicePlatformMapper devicePlatformMapper;

    @Override
    public Map<Integer, List<AppDevicePlatformDTO>> getDevicePlatform() throws BizException {
        List<AppDevicePlatformDTO> platformDTOList = devicePlatformMapper.selectAll();
        return CollectionUtil.groupBy(platformDTOList, AppDevicePlatformDTO::getDeviceType);
    }

    @Override
    public List<AppDevicePlatformDTO> getDevicePlatform(Integer deviceType) throws BizException {
        return devicePlatformMapper.selectByDeviceType(deviceType);
    }

    @Override
    public PageData<AppDevicePlatformDTO> getDevicePlatform(Integer deviceType, Integer pageNum, Integer pageSize) throws BizException {
        PageHelper.startPage(pageNum, pageSize);
        List<AppDevicePlatformDTO> list = devicePlatformMapper.selectByDeviceType(deviceType);
        return PageUtil.pageData(PageInfo.of(list));
    }

    @Override
    public boolean existsDevicePlatform(Integer deviceType, String platform) throws BizException {
        AppDevicePlatformDTO platformDTO = devicePlatformMapper.selectByDeviceTypeAndPlatform(deviceType, platform);
        return platformDTO != null;
    }

    @Override
    public void addDevicePlatform(AppDevicePlatformDTO platformDTO) throws BizException {
        Preconditions.checkNotNull(platformDTO, "");
        Preconditions.checkNotNull(platformDTO.getDeviceType(), "硬件类型不能为空");
        Preconditions.checkNotNull(platformDTO.getPlatform(), "硬件平台编码不能为空");
        Preconditions.checkNotNull(platformDTO.getName(), "硬件平台名称不能为空");
        AppDevicePlatformDTO exist = devicePlatformMapper.selectByDeviceTypeAndPlatform(platformDTO.getDeviceType(), platformDTO.getPlatform());
        if (exist == null) {
            AppDevicePlatform platform = new AppDevicePlatform();
            BeanUtils.copyProperties(platformDTO, platform);
            devicePlatformMapper.insertSelective(platform);
        }
    }

    @Override
    public void editDevicePlatform(AppDevicePlatformDTO platformDTO) throws BizException {
        Preconditions.checkNotNull(platformDTO, "");
        Preconditions.checkNotNull(platformDTO.getDeviceType(), "硬件类型不能为空");
        Preconditions.checkNotNull(platformDTO.getPlatform(), "硬件平台编码不能为空");
        Preconditions.checkNotNull(platformDTO.getName(), "硬件平台名称不能为空");
        AppDevicePlatformDTO exist = devicePlatformMapper.selectByDeviceTypeAndPlatform(platformDTO.getDeviceType(), platformDTO.getPlatform());
        if (exist != null) {
            devicePlatformMapper.updateByDeviceTypeAndPlatform(platformDTO);
        }
    }

    @Override
    public void delDevicePlatform(Integer deviceType, String platform) throws BizException {
        devicePlatformMapper.logicDeleteByDeviceTypeAndPlatform(deviceType, platform);
    }
}
